from flask import Flask,render_template,request
import pymysql

app=Flask(__name__)
@app.route("/add_user",methods=["GET","POST"])
def add_user():
    if request.method=="GET":
        return render_template("add_user.html")
    user = request.form.get("user")
    pwd = request.form.get("pwd")
    mobile = request.form.get("mobile-num")
    print(user,pwd,mobile)

    conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",password="yafeilu",charset="utf8",db="unicom")
    cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql= "insert into admin(username,password,mobile) values (%s,%s,%s)"
    cursor.execute(sql,[user,pwd,mobile])
    conn.commit()
    cursor.close()
    conn.close()
    return "提交成功"
@app.route("/show_table",methods=["GET","POST"])
def show_table():
    if request.method=="GET":
        return render_template("show_table.html")
    user = request.form.get("user")
    conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",password="yafeilu",charset="utf8",db="unicom")
    cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql="select * from admin where username=%s"
    cursor.execute(sql,[user,])
    res=cursor.fetchall()
    print(res)
    return render_template("show_table.html",title="查询结果",data_list=res)



if __name__ == '__main__':
   app.run()
